package com.sanyang.pda.presenter;

import com.sanyang.pda.bean.TakeStockBean;
import com.sanyang.pda.contract.StockStatisticsDetailContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseRowsAppEntity;
import com.wangshen.base.ui.mvp.base.refresh.BaseRecyclerRefreshPresenter;
import com.wangshen.base.ui.mvp.base.refresh.RequestCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class StockStatisticsDetailPresenter extends BaseRecyclerRefreshPresenter<StockStatisticsDetailContract.View, TakeStockBean> implements  StockStatisticsDetailContract.Presenter{
    String storagebinId;
    String taskNo;

    public String getTaskNo() {
        return taskNo;
    }

    public void setTaskNo(String taskNo) {
        this.taskNo = taskNo;
    }

    public String getStoragebinId() {
        return storagebinId;
    }

    public void setStoragebinId(String storagebinId) {
        this.storagebinId = storagebinId;
    }

    @Override
    public void onRefresh(RequestCallback<TakeStockBean> callback) {
        statisticalDetails(callback);
    }

    @Override
    public void onLoadMore(RequestCallback<TakeStockBean> callback) {
        statisticalDetails(callback);
    }
    @Override
    public void stockList(int num,RequestCallback<TakeStockBean> callback) {
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("storagebinId",storagebinId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), jsonObject.toString());
        NetUtils.geApi().stockPageList(num,10,requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<TakeStockBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<TakeStockBean>> listBaseRowsAppEntity) throws Exception {
                        callback.onSuccess(listBaseRowsAppEntity.getRows());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        callback.onFail(throwable.getMessage());
                    }
                });
    }

    @Override
    public void statisticalDetails(RequestCallback<TakeStockBean> callback) {
        NetUtils.geApi().statisticalDetails(taskNo, storagebinId)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<TakeStockBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<TakeStockBean>> listBaseRowsAppEntity) throws Exception {
                        callback.onSuccess(listBaseRowsAppEntity.getRows());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        callback.onFail(throwable.getMessage());
                    }
                });
    }
}
